/*
 * @Author: 朱安武 549410045@qq.com
 * @Date: 2023-01-11 16:06:47
 * @LastEditors: 朱安武 549410045@qq.com
 * @LastEditTime: 2023-02-03 15:21:35
 * @FilePath: \mobile\src\services\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { LocationQueryValue } from "vue-router";
import request from "../utils/request";

type Result<T> = {
    data: {
        code: string,
        message: string,
        data?: T
    }
}

//注册请求
export function sendSms(tel: string) {
    return request.post<any, Result<number>>('user/dosendmsgcode', tel)
}

//验证手机号
export function checkPhone(tel: string) {
    return request.post<any, Result<number>>('user/docheckphone', tel)
}

type ResultLogin = {
    data: {
        code: string,
        message: string,
        data: {
            token: string,
            userid: string
        }
    }
}

//请求登录
export function loginUser(tel: string, password: string) {
    return request.post<any, ResultLogin>('user/login', {
        tel,
        password
    })
}

//定义图片类型
export type Banner = {
    bannerid: string
    img: string
    alt: string
    link: string
}
//查看图片
export function checkImg() {
    return request.get<any, Result<Banner[]>>('banner/list')
}

//获取数据
export function getData(count: number) {
    return request.get(`/pro/list?count=${count}`)
}
//获取详情数据
export function getDetail(id: LocationQueryValue | LocationQueryValue[]) {
    return request.get(`pro/detail/${id}`)
}

//获取推荐数据
export function getLike(count: number = 1) {
    return request.get(`pro/recommendlist?count=${count}`)
}

//购物车请求
export function carData(proid: any) {
    return request.post('cart/add', {
        token: localStorage.getItem('token'),
        userid: localStorage.getItem('userid'),
        proid,
        data: 1
    })
}
//查看购物车
export function checkCar() {
    return request.post('cart/list', {
        token: localStorage.getItem('token'),
        userid: localStorage.getItem('userid'),
    })
}
//删除购物车单条数据
export function removeCar(cartid: any) {
    return request.post('cart/remove', {
        token: localStorage.getItem('token'),
        cartid
    })
}
//更新购物车选中状态
export function checkCardata(cartid: any, flag: any) {
    return request.post('cart/selectone', {
        token: localStorage.getItem('token'),
        cartid,
        flag,
    })
}
//更新所有选中状态
export function checkCarAlldata(type: any) {
    return request.post('cart/selectall', {
        token: localStorage.getItem('token'),
        userid: localStorage.getItem('userid'),
        type
    })
}
//更新数量
export function numChange(cartid: any, num: number) {
    return request.post('cart/updatenum', {
        token: localStorage.getItem('token'),
        cartid,
        num
    })
}